package com.jfinal.complex.tablesplit.impl;

import java.util.List;

import com.jfinal.complex.tablesplit.abst.AbstractTableSplitFactory;
import com.jfinal.complex.tablesplit.model.interfaces.ITableSplitCfg;
import com.jfinal.complex.tablesplit.model.interfaces.ITableSplitMappingCfg;
import com.jfinal.complex.tablesplit.service.interfaces.ITableSplitService;

/**
 * 默认分表工厂
 * @author  约黄昏 yuehhfeng@foxmail.com
 *
 */
public class DefaultTableSplitFactory extends AbstractTableSplitFactory {

	private ITableSplitService tableSplitService;
	
	public DefaultTableSplitFactory(){
		super();
	}
	
	public DefaultTableSplitFactory(ITableSplitService tableSplitService){
		super();
		this.tableSplitService = tableSplitService;
	}
	
	/**
	 * 如果从内存中取配置信息则调用ITableSplitService实例的getTableSplitCfgFromCache()方法
	 * 否则调用getTableSplitCfgFromDB()方法
	 */
	public ITableSplitCfg getTableSplit(String tableName) {
		if(this.isUseCache()){
			return tableSplitService.getTableSplitCfgFromCache(tableName);
		}else{
			return tableSplitService.getTableSplitCfgFromDB(tableName);
		}
	}

	/**
	 * 如果从内存中取配置信息则调用ITableSplitService实例的getTableSplitMappingCfgFromCache()方法
	 * 否则调用getTableSplitMappingCfgFromDB()方法
	 */
	public List<ITableSplitMappingCfg> getTableSplitMapping(String tableName) {
		if(this.isUseCache()){
			return tableSplitService.getTableSplitMappingCfgFromCache(tableName);
		}else{
			return tableSplitService.getTableSplitMappingCfgFromDB(tableName);
		}
	}

	/**
	 * 获取一个新的DefaultTableSplitFactory实例
	 * @return
	 */
	public static DefaultTableSplitFactory getInstance(){
		return new DefaultTableSplitFactory();
	}
	/**
	 * 获取一个新的DefaultTableSplitFactory实例
	 * @return
	 */
	public static DefaultTableSplitFactory getInstance(ITableSplitService tableSplitService){
		return new DefaultTableSplitFactory(tableSplitService);
	}
	
	public DefaultTableSplitFactory setTableSplitService(ITableSplitService tableSplitService){
		this.tableSplitService = tableSplitService;
		return this;
	}
}
